package com.glsc.ngateway.flowable.service;

import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableDetail;
import com.glsc.ngateway.common.base.repo.mysql.flowable.FlowableDetailRepo;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FlowableDetailService {

    private static final Logger logger = LoggerFactory.getLogger(FlowableDetailService.class);

    @Autowired
    FlowableDetailRepo flowableDetailRepo;

    @Transactional
    public void addProcessInstanceByCreate(ProcessInstance processInstance, String sysid, String content, String username, String title) {
        FlowableDetail fd = new FlowableDetail();
        fd.setProcessInstanceId(processInstance.getProcessInstanceId());
        fd.setProcessDefineName(processInstance.getProcessDefinitionName());
        fd.setSysid(sysid);
        fd.setTitle(title);
        fd.setContent(content);
        fd.setProcessDefineId(processInstance.getProcessDefinitionId());
        fd.setTaskName("start");
        fd.setInterfaceName("create");
        fd.setUsername(username);
        flowableDetailRepo.save(fd);
    }

    @Transactional
    public void addProcessInstanceByTask(Task task, ProcessInstance processInstance, String sysid, String content, String title, String interfaceName) {
        FlowableDetail fd = new FlowableDetail();
        fd.setProcessInstanceId(task.getProcessInstanceId());
        fd.setProcessDefineName(processInstance.getProcessDefinitionName());
        fd.setTaskId(task.getId());
        fd.setTitle(title);
        fd.setTaskName(task.getName() + "|" + task.getTaskDefinitionKey());
        fd.setSysid(sysid);
        fd.setContent(content);
        fd.setInterfaceName(interfaceName);
        fd.setProcessDefineId(task.getProcessDefinitionId());
        fd.setUsername(task.getAssignee());
        flowableDetailRepo.save(fd);
    }


    public String queryFormData(String taskId) {
        return flowableDetailRepo.queryFormData(taskId);
    }

    public String queryStartFormData(String processId) {
        return flowableDetailRepo.queryStartDetailByProcessId(processId);
    }

    public String queryLatestFormData(String processId) {
        List<FlowableDetail> list = flowableDetailRepo.queryDetailByProcessId(processId);
        return list.get(0).getContent();
    }

    public List<FlowableDetail> findDetailByProcessId(String processId) {
        List<FlowableDetail> list = flowableDetailRepo.queryDetailByProcessId(processId);
        return list;
    }

    @Transactional
    public void batchUpdateStatus(List<FlowableDetail> flowableDetails, String status) {
        for (FlowableDetail de : flowableDetails) {
            de.setProcessStatus(status);
            flowableDetailRepo.save(de);
        }
    }

    @Transactional
    public void batchUpdateTitle(String processId, String title) {
        List<FlowableDetail> list = flowableDetailRepo.queryDetailByProcessId(processId);
        for (FlowableDetail de : list) {
            de.setTitle(title);
            flowableDetailRepo.save(de);
        }
    }

    public List<FlowableDetail> findAllByStatus(String s) {
        return flowableDetailRepo.findAllByProcessStatus(s);
    }

    public List<FlowableDetail> findAllByProcessIdIn(List<String> ids, String statusNot) {
        return flowableDetailRepo.findAllByProcessIdIn(ids, statusNot);
    }
}